//
//  AppController.m
//  AMT media manager
//
//  Created by gift ku on 2/2/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AppController.h"
#import "CustomControls.h"
#import "Util.h"
#import "AMTMP3Process.h"


@implementation AppController
//@synthesize mediaTableView;
+ (void)initialize
{
	[Utils initAMTDir];
	[Utils initDeviceDir];
}


- (id)init
{
    //TRACE(@"%s", __PRETTY_FUNCTION__);
    if (self = [super init]) {
       		
        _defaults = [NSUserDefaults standardUserDefaults];
		
		treeData = [[SimpleTreeNode treeFromDictionary] retain];
		[self myInitialize];
//		NSThread *myThread = [[NSThread alloc]initWithTarget:self selector:@selector(updateSourceListThread:) object:treeData];
//		[myThread start];
    }
    return self;
}

- (void)updateButtion:(id)sender
{
	[SimpleTreeNode updateSourceList:treeData];
	[outlineView reloadData];
}
- (void) updateSourceListThread:(SimpleTreeNode *)rootNode
{
	//每隔5秒钟检测一次
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
	do {
		[NSThread sleepForTimeInterval:5.0];
		NSLog(@"0000000000000000000000000000000 begin");
		[SimpleTreeNode updateSourceList:rootNode];
		NSLog(@"0000000000000000000000000000000 end");
		[outlineView reloadData];
	} while (YES);
	[pool release];
}

- (void) copyFileThread:(NSArray *)params {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
	
	NSArray *filenames = [params objectAtIndex:0];
	NSNumber *numberTMP = [params objectAtIndex:1];
//	int type = [params objectAtIndex:1];
//	NSInteger
	int type = [numberTMP intValue];
	NSString *toPath = [params objectAtIndex:2];
	
	NSMutableArray *allowCopyFiles = [[NSMutableArray alloc]init];
	NSMutableArray *allowConvertFiles = [[NSMutableArray alloc]init];
	NSMutableArray *denyFiles = [[NSMutableArray alloc]init];
	
	for (NSString *filename in filenames)
	{
		int retVal = [Utils fileTypeFilter:filename type:type];
		if(retVal == dragDeal_copy) {
			[allowCopyFiles addObject:filename];
		} else if(retVal == dragDeal_convert) {
			[allowConvertFiles addObject:filename];
		} else if(retVal == dragDeal_deny) {
			[denyFiles addObject:filename];
		}
	}

	if(type == fileType_music) {
		//调用转码的接口完成格式编码转换	姚
		if(!mp3Process)
		{
			/****
			 参数需要根据设置传入
			 *****/
			mp3Process = [[AMTMP3Process alloc] initWithParam:nil withFrequency:nil];
		}
		/****
		 设置转换参数
		 *****/
		[mp3Process setParam:@"128" withFrequency:@"44.1"];
		[mp3Process convertSomeToMP3:allowConvertFiles to:toPath];
		
	} else if(type == fileType_movie) {
		//调用转码的接口完成格式编码转换	姚
	} else if(type == fileType_picture) {
		//直接拷贝支持的文件
	}
	for(NSString *allowCopyFile in allowCopyFiles)
	{
		[Utils copyFileWithName:allowCopyFile AndType:type AndToPath:toPath];
	}
		//根据文件类型，对文件进行过滤，比如从外部拖入到Music的文件列表中，存在非音频格式的，则不处理，即不添加到列表中去。
		//在这里通过路径和文件名，获取时间和ID3信息，添加到AMTFile类中。
		
		//	//遍历子目录，过滤，拷贝当前Table所支持的格式文件。(弹出对话框，提示，当前的进度，转换中，或者拷贝中，什么的。提供暂停和停止按钮)
		//	//为了能使前台界面可以继续操作，这里需要启动线程，完成转换和拷贝工作，前台可以在对话框中控制进程的运行或者暂停和停止。
	//	[Utils copyFileWithName:filename AndType:type AndToPath:toPath];
		//		[self acceptFilenameDrag:filename AndType:type AndToPath:toPath];
	
	//判断是否为拖动之后那个目录，是否需要转向那个目录
	[self initTableViewWithPath:currentPath];
	[params release];
	[pool release];
}

- (void)myInitialize
{
	//////////
	fileArray = [[NSMutableArray alloc] init];
	
	NSString *amtDir = [[NSHomeDirectory()
						 stringByAppendingPathComponent:@"Music"]
						stringByAppendingPathComponent:@"AMT"];
	NSString *musicDir = [amtDir stringByAppendingPathComponent:@"Music"];
	
	currentType = fileType_music;
	currentPath = [musicDir retain];
	[self initTableViewWithPath:musicDir];
//	[musicDir release];
}

- (void)awakeFromNib
{
//	NSInteger RepeatMode = [_defaults integerForKey:@"RepeatMode"];
//	[menu setRepeatMode:RepeatMode];
	
	[_volumeSlider replaceCell:[CustomSliderCell class]];
    CustomSliderCell* cell = [_volumeSlider cell];
    [cell setImageName:@"MainVolume" backColor:nil trackOffset:5.0 knobOffset:2.0];
	
	///////john add
	NSArray *levelOneNodes = [treeData children];
	for(SimpleTreeNode *libDevNode in levelOneNodes)
	{
		[outlineView expandItem:libDevNode];
	}	
	[outlineView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,DragDropTableCellPboardType, nil]];
	[mediaTableView setTarget: self];
	[mediaTableView setDoubleAction: @selector(doubleClicked:)];
	// Register to accept filename drag/drop
	[mediaTableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,DragDropTableCellPboardType, nil]];
}

///////////////////////////////
- (void)dealloc {
	if(mp3Process)	[mp3Process release];
	[fileArray release];
    [treeData release];
	[currentPath release];
    [super dealloc];
}
@end
